import java.util.*;
public class Lab10Task01 {
public static void main(String[] args) {
instruction();
try {
Scanner kb= new Scanner(System.in);
System.out.print("Enter the value of n [0 <= n <= 143]: ");
int n=kb.nextInt();
System.out.printf("The approximate factorial of %d is %.6e",n,factorial(n));
kb.close();
}
catch(InputMismatchException e) {
System.out.print("Error: "+e);}
catch(IllegalArgumentException e) {
System.out.print("Error: "+e);}

}
public static void instruction() {
System.out.print("This program computes the approximate value of the factorial using the formula proposed by R. W. Gosper. To use this program, the input should be non-negative. Also use values not greater than 143\n\n");
}
public static double factorial(int n)throws IllegalArgumentException {
double ni;
if(n<0||n>143)
throw new IllegalArgumentException();
else if(n==0)
ni=1;
else {
ni=Math.pow(n, n)*Math.pow(Math.E, -n)*Math.sqrt((2*n+1.0/3.0)*Math.PI);
}
return ni;
}
}
